/* eslint-disable */
/**
 * 打印相关工具类
 */
//ISO 216纸张尺寸（单位：mm）
const paperSizeMap = {
  //A系列格式
  A0: {width:841, height:1189},
  A1: {width:594, height:840},
  A2: {width:420, height:594},
  A3: {width:297, height:420},
  A4: {width:210, height:297},
  A5: {width:148, height:210},
  A6: {width:105, height:148},
  A7: {width:74, height:105},
  A8: {width:52, height:74},
  A9: {width:37, height:52},
  A10: {width:26, height:37},

  //B系列格式
  B0: {width:1000, height:1414},
  B1: {width:706, height:1000},
  B2: {width:500, height:707},
  B3: {width:353, height:500},
  B4: {width:250, height:353},
  B5: {width:176, height:250},
  B6: {width:125, height:176},
  B7: {width:88, height:125},
  B8: {width:62, height:88},
  B9: {width:44, height:62},
  B10: {width:31, height:44},

  //C系列格式
  C0: {width:917, height:1297},
  C1: {width:648, height:917},
  C2: {width:458, height:648},
  C3: {width:324, height:458},
  C4: {width:229, height:324},
  C5: {width:162, height:229},
  C6: {width:114, height:162},
  C7: {width:81, height:114},
  C8: {width:57, height:81},
  C9: {width:40, height:57},
  C10: {width:28, height:40 },

  //JIS
  B0JIS: {width:1030, height:1456},
  B1JIS: {width:728, height:1030},
  B2JIS: {width:515, height:728},
  B3JIS: {width:364, height:515},
  B4JIS: {width:257, height:364},
  B5JIS: {width:182, height:257},
  B6JIS: {width:128, height:182},
  B7JIS: {width:91, height:128},
  B8JIS: {width:64, height:91},
  B9JIS: {width:45, height:64},


}

const utilPrint = {
  /**打开表单打印窗口
   * @param   param 相关参数
   * @param   type  类型，dataview或者不传为数据视图打印,report:报表打印,user:用户自定义打印
   */
  open(param, type){
    if(type && (type !='dataview' && 'report,user,set'.indexOf(type) == -1 )){
      global.utilMessage.alertFail(`打印类型【${type}】尚未实现!`)
      return
    }

    //已打印，进行提示
    if (param.tableName && param.pkName && param.pkValue && param.form &&  global.strings.isNotBlank(param.form.printTime)) {
      global.utilMessage.confirm(`该单据已打印是否重打？<br>最近打印时间:${param.form.printTime}`, () => {
        param.type = type
        let home = global.vue.$children[0].$children[0]
        home.dialogPrintForm = true
        home.printFormParam = param
        home.dialogPrintTitleAppend = ''
        if(param.title){
          home.dialogPrintTitleAppend ='_'+ param.title
        }
      })
    } else {
      param.type = type
      let home = global.vue.$children[0].$children[0]
      home.dialogPrintForm = true
      home.printFormParam = param
      home.dialogPrintTitleAppend = ''
      if(param.title){
        home.dialogPrintTitleAppend ='_'+ param.title
      }
    }
  },

  /**打开报表打印窗口
   * @param   param 相关参数
   * @param   type  类型，dataview或者不传为数据视图打印,report:报表打印,user:用户自定义打印
   */
  openReport(paramArr){
    let home = global.vue.$children[0].$children[0]
    home.dialogPrintReportForm = true
    home.printReportFormParam = paramArr
  },

  /**打开导入窗口
   * @param   param 相关参数
   * @param   type  类型，dataview或者不传为数据视图打印,report:报表打印,user:用户自定义打印
   */
  openImport(param){
    let home = global.vue.$children[0].$children[0]
    home.dialogImport = true
    home.dialogImportParam = param
  },

  /**打开仓库-库存作业-标签打印窗口
   * @param   propLineId  入库明细id
   * @param   propWorkOrderKey  入库单号
   * @param   propSnArr  sn数组
   */
  openPrintLabel(propLineId,propWorkOrderKey,propSnArr){
    let home = global.vue.$children[0].$children[0]
    home.propLineId = propLineId
    home.propWorkOrderKey = propWorkOrderKey
    home.propSnArr = propSnArr
    home.setBarcodeVisible = true
    home.drawerSnPrint = true

  },

  /**打开动态窗口
   * @type   type  report,dataview
   * @typeKey   视图标识或者报表标识
   * @queryWrapper   视图标识或者报表查询条件
   * @dialogMoreTitle   窗口标题
   * @dialogMoreViewForm   数据视图时，编辑窗口地址
   * @closeCall       窗口关闭后回调函数
   */
  openDialog(type,typeKey,queryWrapper,dialogMoreTitle,dialogMoreViewForm,  closeCall, rowEditParam){
    let home = global.vue.$children[0].$children[0]
    home.dialogMoreType = type
    home.dialogMoreTypeKey = typeKey
    home.dialogMoreWrapper = queryWrapper
    home.dialogMoreTitle = dialogMoreTitle
    home.dialogMoreForm = true
    home.dialogMoreViewForm = dialogMoreViewForm
    home.dialogMoreRowEditParam = rowEditParam
    window.dialogMoreCloseCall = null
    if (closeCall && typeof closeCall === "function") {
      window.dialogMoreCloseCall = closeCall
    }
  },


  /**更新标签打印数量即最近打印时间
   * @param   param 相关参数
   */
  updatePrintNum(snArr){
    global.$ajax.post(`/wm/stock/updatePrintNum`,{snArr: snArr} ).then((response) => {
      if(response.code == 0 ){
      }
    })
  },
  /**更新标签打印数量即最近打印时间
   * @param   param 相关参数
   */
  updateFormPrintTime(tableName,pkName,pkValue){
    global.$ajax.get(`/tool/updateFormPrintTime/${tableName}/${pkName}/${pkValue}`).then((response) => {
    })
  },

  /**标签打印窗口
   * @param   param 相关参数
   */
  openLabel(param){
    let home = global.vue.$children[0].$children[0]
    home.printLabelFormParam = param
    home.$refs.customDrawerPrintLabel.show()
    home.$refs.printLabelInst.initOut()
  },

  /**检查打印客户端依赖依赖
   */
  isNeedCLodopfuncs(printServer, call, that){
    global.utilVue.loadShow(true)
    var head = document.head || document.getElementsByTagName("head")[0] || document.documentElement;
    var oscript = document.createElement("script");

    if (!printServer) printServer = "localhost"
    oscript.src ="http://"+printServer+":8000/CLodopfuncs.js?priority=1";
    try {
      let isLoad = false
      setTimeout(() => {
        if (!isLoad) {
          global,utilMessage.warning(`打印客户端无法访问，客户端IP为${printServer}`)
          console.log(`打印客户端无法访问，客户端IP为${printServer}`)
        }
        global.utilVue.loadHide()
      }, 2000)

      head.insertBefore( oscript,head.firstChild );
      //引用双端口(8000和18000）避免其中某个被占用：
      oscript = document.createElement("script");
      oscript.src ="http://"+printServer+":18000/CLodopfuncs.js?priority=0";

      if(oscript.readyState){   //IE
        oscript.onreadystatechange=function(){
          if(script.readyState=='complete'||script.readyState=='loaded'){
            isLoad = true
            global.utilVue.loadHide()
            if (call) call(that)
          }
        }
      }else{    //非IE
        oscript.onload=function(){
          isLoad = true
          global.utilVue.loadHide()
          if (call) call(that)
        }
      }
      head.insertBefore( oscript,head.firstChild );
    }catch (e) {
      console.error('e', e)
    }
  },

  /**获取纸张宽高，返回宽高单位为毫米
   * @param paperName 纸张名称，不区分大小写，eg: A4
   * @return 找到返回 {width:100,height:200}，未找到:返回null
   */
  getPaperSize(paperName){
    if(!paperName) return null
    paperName = paperName.toUpperCase()
    paperName = paperName.replace(/\s+/g,"")
    //替换所有括号
    paperName = paperName.replace(/\([^\)]*\)/g,"");
    return paperSizeMap[paperName]
  },

  //更新工作台打印设置，暂时用jquery方式实现
  updateWorkSet(){
    // $("#work_set_printer_form_name").val(localStorage.getItem('printer_form_name'))
    // $("#printer_label_server_name").val(localStorage.getItem('printer_label_server_name'))
    // $("#printer_label_server_name_out").val(localStorage.getItem('printer_label_server_name'))
  },

  /**打开树列表窗口
   * @param   param 相关参数
   */
  openTreeList(param){
    let home = global.vue.$children[0].$children[0]
    if (param) {
      Object.keys(param).forEach(key => {
        home[key] = param[key]
      })
    }

    home.dialogTreeList = true
  },
  /**打开报表打印窗口
   * @param   param 相关参数
   * @param   type  类型，dataview或者不传为数据视图打印,report:报表打印,user:用户自定义打印
   */
  openPdf(pdfUrl){
    let home = global.vue.$children[0].$children[0]
    let typeName=pdfUrl.substring(pdfUrl.lastIndexOf('.')+1);
    if (typeName == 'docx') {
      global.utilVue.loadShow(true)
      global.$ajax.get(`/tool/getPdfFromDocx?docxPath=${pdfUrl}` ).then((response) => {
        if(response.code == 0 ){
          home.dialogPDFForm = true
          home.pdfUrl = response.data
          let baseApi = location.origin+'/api'
          if (response.data && response.data.indexOf('http') == -1)   home.pdfUrl = baseApi + response.data
        }
        global.utilVue.loadHide()
      })

    } else {
      home.dialogPDFForm = true
      home.pdfUrl = pdfUrl
    }
  },

  /**打开表单打印窗口
   * @param   param 相关参数
   * @param   type  类型，dataview或者不传为数据视图打印,report:报表打印,user:用户自定义打印
   */
  openFormPrint(param){
    let home = global.vue.$children[0].$children[0]
    home.dialogPrintForm = true
    home.printFormParam = param
    home.dialogPrintTitleAppend = ''
    if(param.title){
      home.dialogPrintTitleAppend ='_'+ param.title
    }
  },

  /**pdf预览
   * @param   title 标题
   * @param   src  地址
   * @param   pageSize  纸张大小 A4  A5
   * @param   direction  纸张方向 transverse 横向   longitudinal纵向
   * @param   spacingMode  间距模式 normal正常间距  no_spacing 无间距
   * @param   perRowsOfGrid  每页数量 默认为20
   */
  openPdfView(
    title,
    src,
    pageSize,
    direction,
    spacingMode,
    perRowsOfGrid
  ){
    let home = global.vue.$children[0].$children[0]
    home.pdfview.title = title
    if (pageSize) {
      home.pdfview.pageSize = pageSize
    }
    if (direction) {
      home.pdfview.direction = direction
    }
    if (spacingMode) {
      home.pdfview.spacingMode = spacingMode
    }
    if (perRowsOfGrid) {
      home.pdfview.perRowsOfGrid = perRowsOfGrid
    }
    home.pdfview.src = src
    home.$refs.customDrawerPdfView.show()
  },
}

export default utilPrint
